package com.rpa.server.servicerpaserver.crawler.controller;

import com.rpa.secure.servicerpasecure.common.constant.MessageConstant;
import com.rpa.secure.servicerpasecure.common.exception.TaskException;
import com.rpa.secure.servicerpasecure.common.result.Result;
import com.rpa.secure.servicerpasecure.common.util.CurrentTeacherInfoUtil;
import com.rpa.server.servicerpaserver.crawler.entity.dto.CrawlerDto;
import com.rpa.server.servicerpaserver.crawler.operate.CourseOperate;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @className: CourseController
 * @author: lzt
 * @date: 2025/1/10 14:37
 * @Version: 1.0
 * @description:
 */
@Tag(name = "获取课程相关接口", description = "CourseCrawlerController")
@Slf4j
@RestController
@RequestMapping("/crawler")
public class CourseCrawlerController {
    @Autowired
    private CourseOperate courseOperate;
    @PostMapping("/getNameAndPicture")
    @Operation(summary = "获取/更新头像", description = "getNameAndPicture")
    public Result<Boolean> getNameAndPicture(@RequestBody CrawlerDto crawlerDto){
        // 检查 user 对象是否为空
        if (crawlerDto == null) {
            log.error("请求体中的 user 对象为空");
            throw new TaskException(MessageConstant.INVALID_REQUEST_BODY);
        }
        String rpaRegisterId =  crawlerDto.getAccountId();

        // 检查 rpaRegisterId 是否为空
        if (rpaRegisterId == null) {
            log.error("请求体中的 rpaRegisterId 字段为空");
            throw new TaskException(MessageConstant.INVALID_REQUEST_BODY);
        }

        log.info("当前用户id为：{}", CurrentTeacherInfoUtil.getCurrentTeacherId());
        log.info("接收到的 rpaRegisterId 为：{}", rpaRegisterId);

        if (rpaRegisterId.equals(CurrentTeacherInfoUtil.getCurrentTeacherId())) {
            log.info("校验通过");
            return Result.success(courseOperate.getNameAndPicture(rpaRegisterId).join());
        } else {
            log.error("校验失败！rpaRegisterId 不匹配");
            throw new TaskException(MessageConstant.ULTRA_VIRES);
        }
    }

    @PostMapping("/getCourse")
    @Operation(summary = "获取/更新课程", description = "getCourse")
    public Result<Boolean> getCourse(@RequestBody CrawlerDto crawlerDto){
        // 检查 user 对象是否为空
        if (crawlerDto == null) {
            log.error("请求体中的 user 对象为空");
            throw new TaskException(MessageConstant.INVALID_REQUEST_BODY);
        }
        String rpaRegisterId =  crawlerDto.getAccountId();

        // 检查 rpaRegisterId 是否为空
        if (rpaRegisterId == null) {
            log.error("请求体中的 rpaRegisterId 字段为空");
            throw new TaskException(MessageConstant.INVALID_REQUEST_BODY);
        }

        log.info("当前用户id为：{}", CurrentTeacherInfoUtil.getCurrentTeacherId());
        log.info("接收到的 rpaRegisterId 为：{}", rpaRegisterId);

        return Result.success(courseOperate.getCourse(rpaRegisterId).join());
    }

}
